<?xml version="1.0" encoding="utf-8" ?>
<xsl:stylesheet
    version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:soap="http://www.w3.org/2003/05/soap-envelope"
    xmlns:dssp="http://schemas.microsoft.com/xw/2004/10/dssp.html"
	  xmlns:sd21sonarpan="http://www.robot-electronics.co.uk/2006/10/sd21sonarpan.html"
	  xmlns:phys="http://schemas.microsoft.com/robotics/2006/07/physicalmodel.html"
		xmlns:sonar="http://schemas.microsoft.com/robotics/2006/06/sonar.html"    >

  <xsl:import href="/resources/dss/Microsoft.Dss.Runtime.Home.MasterPage.xslt" />

  <xsl:template match="/">
    <xsl:comment>
      <!-- Service Header Info -->
    </xsl:comment>
    <xsl:variable name="title">
      SD21 Sonar Pan Service
    </xsl:variable>
    <xsl:variable name="serviceName">
      SD21 Sonar Pan Service
    </xsl:variable>
    <xsl:variable name="description">
      Does a timed move of a servo making use of the speed command to avoid having to control each step of a move, and stores sonar pings according to the time received. <br/><br/>
      If you see gaps in the otherwise smooth gradiant, then you are moving the servo too fast, or not pinging fast enough.<br/><br/>
      Makes available all the incoming data as a new Generic Sonar with pose updated to reflect the position of the sonar with respect to the servo and the servo offset from the robot.

    </xsl:variable>

    <xsl:call-template name="MasterPage">
      <xsl:with-param name="serviceName" select="$serviceName" />
      <xsl:with-param name="description" select="$description" />
      <!-- If title is not provided, serviceName will be used instead. -->
      <xsl:with-param name="title">
        <xsl:value-of select="$serviceName" />
        <xsl:if test="$title != ''">
          <xsl:text> - </xsl:text>
          <xsl:value-of select="$title" />
        </xsl:if>
      </xsl:with-param>
      <!-- Possible values for navigation are: 'Open', 'Closed', and 'None'
           'Open' is the default value. -->
      <xsl:with-param name="navigation" select="'Open'" />
      <!-- The contents of head param will be placed just before the </head> tag in html. -->
      <xsl:with-param name="head">
        <link rel="stylesheet" type="text/css" href="/resources/I2CHardware/Devantech.Services.I2C.Hardware.resources.DevantechI2CHardware.Stylesheet.css" />

        <style type="text/css">
          /* Service-specific stylesheet goes here */
        </style>
        <script language="javascript" type="text/javascript">
          <![CDATA[<!--

/* Service-specific script goes here */

dssRuntime.init = function()
{
  // Add page initialization code here.
  // This function is attached to the window.onload event.
  // Do not override window.onload.
}

					function op(obj,id2) {
					o(id2).className = 'selected';
					o(id2 + 'Buttons').className = 'submitButtonsSelected';
					b();
					obj.select() ;
					}
					function of( id) {
					o(id).className = 'none';
					o(id + 'Buttons').className = 'submitButtons';
					b();
					}
					function o(id) {
					return document.getElementById(id);
					}
					function b() {
					if (document.selection)
					document.selection.empty();
					else if (window.getSelection)
					window.removeAllRanges();
					}

//-->     ]]>
        </script>
      </xsl:with-param>
    </xsl:call-template>
  </xsl:template>

  <!-- Match service state's document element. -->
	
	<xsl:template match="/sd21sonarpan:SD21SonarPanState">

					<br clear="all" />
				
					<div class="RightColumn">
						<form method="POST">
							<fieldset id="Enable" class="none">
								<legend>Enable</legend>
								<label for="Enabled">Enabled</label>
								<div class="fieldRowRight">
									<select id="Enabled" name="Enabled" type="text" value="" class="Editable" 
										onfocus="op(this, 'Enable');" 
										title="Click to edit. Enables the service to pan and forward sonar messages">
										<xsl:choose>
											<xsl:when test="sd21sonarpan:Enabled = 'true'">
												<option value="True" selected="selected">True</option>
												<option value="False">False</option>
											</xsl:when>
											<xsl:otherwise>
												<option value="True">True</option>
												<option value="False" selected="selected">False</option>
											</xsl:otherwise>
										</xsl:choose>
									</select>
								</div>
                <br clear="right"/>
								<div class="submitButtons" id="EnableButtons">
									<input type="reset" value="Cancel"  onclick="of('Enable');" class="OrangeButton" /> or <input type="submit" value="Change" id="EnableSubmit" class="OrangeButton" />
								</div>
							</fieldset>
						</form>
						
						<form method="POST">
							<fieldset id="SetPanConfig" class="none">
								<legend>Pan Config</legend>
								<div class="helpText">Defines the servo movement.</div>
								<label for="ServoChannel">Servo Channel</label>
								<div class="fieldRowRight">
									<input id="ServoChannel" name="ServoChannel" type="text" class="Editable" 
										onfocus="op(this, 'SetPanConfig');" 
										title="Click to edit. (Int 0-20) The SD21 servo channel.">
										<xsl:attribute name="value">
											<xsl:value-of select="sd21sonarpan:ServoChannel"/>
										</xsl:attribute>
									</input>
								</div>
								<label for="PanTime">Pan Time</label>
								<div class="fieldRowRight">
									<input id="PanTime" name="PanTime" type="text" class="Editable" 
										onfocus="op(this, 'SetPanConfig');" 
										title="Click to edit. (Int) The time in miliseconds of a complete pan.">
										<xsl:attribute name="value">
											<xsl:value-of select="sd21sonarpan:PanTime"/>
										</xsl:attribute>
									</input>
								</div>
								<label for="MinPulseWidth">Min Pulse Width</label>
								<div class="fieldRowRight">
									<input id="MinPulseWidth" name="MinPulseWidth" type="text" class="Editable" 
										onfocus="op(this, 'SetPanConfig');" 
										title="Click to edit. (Int) The minimum value of the pulse width. Below 800 might break your servo.">
										<xsl:attribute name="value">
											<xsl:value-of select="sd21sonarpan:MinPulseWidth"/>
										</xsl:attribute>
									</input>
								</div>
								<label for="MaxPulseWidth">Max Pulse Width</label>
								<div class="fieldRowRight">
									<input id="MaxPulseWidth" name="MaxPulseWidth" type="text" class="Editable" 
										onfocus="op(this, 'SetPanConfig');" 
										title="Click to edit. (Int) The maximum value of the pulse width. Above 2200 might break your servo.">
										<xsl:attribute name="value">
											<xsl:value-of select="sd21sonarpan:MaxPulseWidth"/>
										</xsl:attribute>
									</input>
								</div>
								<div class="submitButtons" id="SetPanConfigButtons">
									<input type="reset" value="Cancel"  onclick="of('SetPanConfig');" class="OrangeButton" /> or <input type="submit" value="Set PanConfig" id="SetPanConfigSubmit" class="OrangeButton" />
								</div>
							</fieldset>
						</form>
						
						<form method="POST">
							<fieldset id="SetDrawScale" class="none">
								<legend>Draw Scale</legend>
								<div class="helpText">The scale of the image.</div>
								<label for="DrawScale">Draw Scale</label>
								<div class="fieldRowRight">
									<input id="DrawScale" name="DrawScale" type="text" class="Editable" 
										onfocus="op(this, 'SetDrawScale');" 
										title="Click to edit. (double) The scale at which to draw the image.">
										<xsl:attribute name="value">
											<xsl:value-of select="sd21sonarpan:DrawScale"/>
										</xsl:attribute>
									</input>
								</div>
								<div class="submitButtons" id="SetDrawScaleButtons">
									<input type="reset" value="Cancel"  onclick="of('SetDrawScale');" class="OrangeButton" /> or <input type="submit" value="Set DrawScale" id="SetDrawScaleSubmit" class="OrangeButton" />
								</div>
							</fieldset>
						</form>
            	
					</div>
					<form method="POST">
						<fieldset id="SetHardwareIdentifiers" class="none">
							<legend>Hardware Identifiers</legend>
							<div class="helpText">The list of Generic Sonars to listen for at startup.</div>
							<label for="HardwareIdentifiers">Hardware Identifiers</label>
							<div class="fieldRowRight">
								<input id="HardwareIdentifiers" name="HardwareIdentifiers" type="text" class="Editable" 
									onfocus="op(this, 'SetHardwareIdentifiers');" 
									title="Click to edit. (List&lt;int&gt;) A comma separated list of hardware identifiers to subscribe to.">
                  <xsl:variable name="numIdentifiers" select="count(sd21sonarpan:HardwareIdentifiers/*)"/>
                  <xsl:attribute name="value">
                    <xsl:for-each select="sd21sonarpan:HardwareIdentifiers/*">
                      <xsl:value-of select="."/>
                      <xsl:if test="position() != $numIdentifiers">, </xsl:if>
                    </xsl:for-each>
                  </xsl:attribute>
									</input>
							</div>

							<div class="submitButtons" id="SetHardwareIdentifiersButtons">
								<input type="reset" value="Cancel"  onclick="of('SetHardwareIdentifiers');" class="OrangeButton" /> or <input type="submit" value="Set HardwareIdentifiers" id="SetHardwareIdentifiersSubmit" class="OrangeButton" />
							</div>
						</fieldset>
					</form>
					
					<form method="POST">
						<fieldset id="SetServoPose" class="none">
							<legend>Servo Pose</legend>
							<div class="helpText">The Pose of the center of rotation with respect to the robot's Pose</div>
							<label for="ServoPosePosX">Position X</label>
							<div class="fieldRowRight">
								<input id="ServoPosePosX" name="ServoPosePosX" type="text"
								class="EditableSmall" onfocus="op(this,'SetServoPose');" title="Click to edit. " >
									<xsl:attribute name="value">
										<xsl:value-of select="sd21sonarpan:ServoPose/phys:Position/phys:X"/>
									</xsl:attribute>
								</input>
								<label for="ServoPosePosY" class="inline">Y</label>
								<input id="ServoPosePosY" name="ServoPosePosY" type="text" 
								class="EditableSmall" onfocus="op(this,'SetServoPose');" title="Click to edit. " >
									<xsl:attribute name="value">
										<xsl:value-of select="sd21sonarpan:ServoPose/phys:Position/phys:Y"/>
									</xsl:attribute>
								</input>
								<label for="ServoPosePosZ" class="inline">Z</label>
								<input id="ServoPosePosZ" name="ServoPosePosZ" type="text"
								class="EditableSmall" onfocus="op(this,'SetServoPose');" title="Click to edit. " >
									<xsl:attribute name="value">
										<xsl:value-of select="sd21sonarpan:ServoPose/phys:Position/phys:Z"/>
									</xsl:attribute>
								</input>
							</div>
							<label for="ServoPoseOriX">Orientation X</label>
							<div class="fieldRowRight">
								<input id="ServoPoseOriX" name="ServoPoseOriX" type="text"
								class="EditableSmall" onfocus="op(this,'SetServoPose');" title="Click to edit. " >
									<xsl:attribute name="value">
										<xsl:value-of select="sd21sonarpan:ServoPose/phys:Orientation/phys:X"/>
									</xsl:attribute>
								</input>
								<label for="ServoPoseOriY" class="inline">Y</label>
								<input id="ServoPoseOriY" name="ServoPoseOriY" type="text"
								class="EditableSmall" onfocus="op(this,'SetServoPose');" title="Click to edit. " >
									<xsl:attribute name="value">
										<xsl:value-of select="sd21sonarpan:ServoPose/phys:Orientation/phys:Y"/>
									</xsl:attribute>
								</input>
								<label for="ServoPoseOriZ" class="inline">Z</label>
								<input id="ServoPoseOriZ" name="ServoPoseOriZ" type="text"
								class="EditableSmall" onfocus="op(this,'SetServoPose');" title="Click to edit. " >
									<xsl:attribute name="value">
										<xsl:value-of select="sd21sonarpan:ServoPose/phys:Orientation/phys:Z"/>
									</xsl:attribute>
								</input>
								<label for="ServoPoseOriZ" class="inline">W</label>
								<input id="ServoPoseOriW" name="ServoPoseOriW" type="text"
							 class="EditableSmall" onfocus="op(this,'SetServoPose');" title="Click to edit. " >
									<xsl:attribute name="value">
										<xsl:value-of select="sd21sonarpan:ServoPose/phys:Orientation/phys:W"/>
									</xsl:attribute>
								</input>
							</div>

							<div class="submitButtons" id="SetServoPoseButtons">
								<input type="reset" value="Cancel"  onclick="of('SetServoPose');" class="OrangeButton" /> or <input type="submit" value="Set ServoPose" id="SetServoPoseSubmit" class="OrangeButton" />
							</div>
						</fieldset>
					</form>

					<xsl:for-each select="sd21sonarpan:Sonars/*">
						<br clear="all" />
            <br/>
						<div class="hr"></div>
						<div class="module">
							<div class="title">Generic Sonar at <xsl:value-of select="sd21sonarpan:State/sonar:HardwareIdentifier" />
              </div>
								<span class="smallText">
								 Last Contact: <xsl:value-of select="sd21sonarpan:State/sonar:TimeStamp" />
								</span>
              <br/>						
							<span class="key">
								Sonar Identifier:
							</span>
							<span class="value">
								<xsl:value-of select="sd21sonarpan:State/sonar:HardwareIdentifier" />
							</span>
							<br/>
							<span class="key">
								Subscribed:
							</span>
							<span class="value">
								<xsl:value-of select="sd21sonarpan:Subscribed" />
							</span>
							<br/>
							<span class="key">
								Sonar Max Range:
							</span>
							<span class="value">
								<xsl:value-of select="sd21sonarpan:State/sonar:MaxDistance" /> (meters)
							</span>
							<br/>
							<span class="key">
								Angular Range:
							</span>
							<span class="value">
								<xsl:value-of select="sd21sonarpan:State/sonar:AngularRange" /> (degrees)
							</span>
							<br/>
							<span class="key">
								Angular Resolution:
							</span>
							<span class="value">
								<xsl:value-of select="sd21sonarpan:State/sonar:AngularResolution" /> (degrees)
							</span>
							<br/>
							<span class="key">
								Sonar Position:
							</span>
							<span class="value">
								(X: <xsl:value-of select="sd21sonarpan:State/sonar:Pose/phys:Position/phys:X" />,
								Y: <xsl:value-of select="sd21sonarpan:State/sonar:Pose/phys:Position/phys:Y" />,
								Z: <xsl:value-of select="sd21sonarpan:State/sonar:Pose/phys:Position/phys:Z" />)
							</span>
							<br/>
							<span class="key">
								Sonar Orientation:
							</span>
							<span class="value">
								(X: <xsl:value-of select="sd21sonarpan:State/sonar:Pose/phys:Orientation/phys:X" />,
								Y: <xsl:value-of select="sd21sonarpan:State/sonar:Pose/phys:Orientation/phys:Y" />,
								Z: <xsl:value-of select="sd21sonarpan:State/sonar:Pose/phys:Orientation/phys:Z" />,
								W: <xsl:value-of select="sd21sonarpan:State/sonar:Pose/phys:Orientation/phys:W" />)
							</span>
							
							
							<br/>
							<br/>
							<img >
								<xsl:attribute name="src">
									<xsl:text>/sd21sonarpan/map</xsl:text>
									<xsl:value-of select="position()-1"/>
									<xsl:text>.png</xsl:text>
								</xsl:attribute>
							</img>
							<br/>
							<span class="key">Distance Measurements:</span>
							<br/>
							<table>
								<tr>
									<xsl:for-each select="sd21sonarpan:DistanceMeasurements/*">
										<td>
											<span class="key">
												<xsl:value-of select="position()"/>:
											</span>
										</td>
									</xsl:for-each>
								</tr>
								<tr>
									<xsl:for-each select="sd21sonarpan:DistanceMeasurements/*">
										<td>
											<span class="value">
												<xsl:value-of select="." />
											</span>
										</td>
									</xsl:for-each>
								</tr>
							</table>
						</div>
					</xsl:for-each>
				<br clear="all" />
				<div class="hr"></div>
				<span class="SmallText">Config at: /store/Devantech.Services.SD21SonarPan.xml</span>
				<br/>
				Configure your Sonar Poll frequency using your sonar hardware page. (Possibly: <a href="/srf08hardware">SRF08 Hardware page</a>, or <a href="/srf235hardware">SRF235 Hardware Page</a>)
				<br/>
				Ensure the limits for Min and Max are within the range set for the servo on the <a href="/sd21hardware">SD21 Hardware page</a>
	</xsl:template>
</xsl:stylesheet>
